package pri.zey.custom.tools.okhttp;/**
 * Created by Administrator on 2016/4/26 0026.
 */

import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

//import com.squareup.okhttp.Call;
//import com.squareup.okhttp.Callback;
//import com.squareup.okhttp.FormEncodingBuilder;
//import com.squareup.okhttp.MediaType;
//import com.squareup.okhttp.OkHttpClient;
//import com.squareup.okhttp.Request;
//import com.squareup.okhttp.Response;
//import com.xincheping.MyApplication;
//import com.xincheping.widget.okhttp.PersistentCookieStore;

import java.io.IOException;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class GoClient {

//    private Class cls;
//    private String tag;
//    private String url;
//    private Map<String, Object> paras;
//    private CusCallback callback;
//
//    public interface CusCallback{
//        void onSuccess(String tag, Response response, String bodyStr);
//        void onFail(String tag, Request request, IOException e);
//    }
//
//    public void getDate(Class cls, String tag, String url, Map<String, Object> paras, CusCallback callback){
//        this.cls = cls;
//        this.tag = tag;
//        this.url = url;
//        this.paras = paras;
//        this.callback = callback;
//        init();
//    }
//
//    private void init(){
//        OkHttpClient client = new OkHttpClient();
//        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
//        try {
//            if(!MyApplication.checkConnection()){
//                MyApplication.show("网络连接失败,请检查网络!");
//                return;
//            }
//            //client.setCookieHandler(new java.net.CookieManager(null, CookiePolicy.ACCEPT_ALL));
//
//            client.setCookieHandler(new java.net.CookieManager(
//                    new PersistentCookieStore(MyApplication.getContext()),
//                    CookiePolicy.ACCEPT_ALL));
//
//            //client.setCookieHandler(CookieHandler cookieHanlder);
//
//            com.squareup.okhttp.Request.Builder builder=new com.squareup.okhttp.Request.Builder();
//            builder=builder.url(url);
//            builder=builder.addHeader("Accept", "application/json; q=0.5");
//
//            FormEncodingBuilder formBuilder = new FormEncodingBuilder();
//            if (paras != null){
//                Iterator iter = paras.entrySet().iterator();
//                while (iter.hasNext()) {
//                    Map.Entry entry = (Map.Entry) iter.next();
//                    String key = entry.getKey().toString();
//                    String val = entry.getValue() != null ? entry.getValue().toString() : "";
//                    formBuilder.add(key, val);
//                }
//                /***
//                 * 加密方法
//                 */
////                formBuilder = new aSecret().getFormBuilder(paras);
//            }
//
//            builder = builder.post(formBuilder.build());
//            com.squareup.okhttp.Request request = builder.build();
//
//            try{
//                Call call = client.newCall(request);
//                call.enqueue(new Callback() {
//                    @Override
//                    public void onFailure(Request request, IOException e) {
//                        callback.onFail(tag, request, e);
//                    }
//
//                    @Override
//                    public void onResponse(Response response) throws IOException {
//                        callback.onSuccess(tag, response, response.body().string());
//                    }
//                });
//
//                PersistentCookieStore persistentCookieStore =new PersistentCookieStore(MyApplication.getContext());
//                try {
//                    List<HttpCookie> list= persistentCookieStore.getCookies();
//                    if(list!=null) {
//                        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(MyApplication.getContext());
//                        cookieSyncManager.sync();
//                        CookieManager cookieManager = CookieManager.getInstance();
//                        cookieManager.setAcceptCookie(true);
//                        for(HttpCookie o:list) {
//                            String cookieStr=o.toString();
//                            cookieStr=cookieStr+ ";";
//                            cookieStr=cookieStr+"domain="+MyApplication.getCookieDomain()+";";
//                            cookieStr=cookieStr+"path=/;";
//                            cookieManager.setCookie(o.getDomain(),cookieStr);
//                        }
//                        cookieSyncManager.sync();
//                    }
//                }catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }catch (Exception e){
//                e.printStackTrace();
//                MyApplication.show("网络连接失败!");
//                //Log.d("MyHttpClient","网络连接失败!");
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            if(client!=null){
//                client=null;
//            }
//        }
//    }
}
